package com.espirit.builder.biz;

import java.util.HashMap;
import java.util.Map;

import org.springframework.stereotype.Component;

import com.espirit.eap.manager.I18n;
import com.espirit.eap.manager.ModelBuilderSupport;
import com.espirit.eap.manager.workflow.WFCondition;
import com.espirit.eap.manager.workflow.WFTemplate;
import com.espirit.eap.pagelayout.FlatSection;
import com.espirit.eap.pagelayout.GridSection;
import com.espirit.eap.pagelayout.ListLayout;
import com.espirit.eap.pagelayout.ModelField;
import com.espirit.eap.pagelayout.SectionElement;
import com.espirit.eap.pagelayout.TabbedSection;
@Component
public class WFTemplateBuilder extends ModelBuilderSupport {
	private static final String FIELD_NAME="name";
	private static final String FIELD_DESCRIPTION = "description";
	private static final String FIELD_WFPHASES="wfPhases";
	private static final String FIELD_WFEVENTS="selectedEvents";
	
	private static final String FIELD_WFCONDITION="wfCondition";
	
	@Override
	public boolean init() {
		return super.setClass(WFTemplate.class, "Workflow Template", "批准模板") ;
	}
	
	@Override
	public void buildFields() {
		addModelField(FIELD_NAME, "Template Name", "模板名称");
		addModelField(FIELD_DESCRIPTION, "Description", "描述");
		addModelField(FIELD_WFPHASES, "Workflow Phases", "批准阶段");
		addModelField(FIELD_WFEVENTS, "Ledger", "凭证");
		addModelField(FIELD_WFCONDITION,"Condtion","条件");
		addModelField("id","Workflow Template","审批模板");
	}
	
	@Override
	public void prepareLayoutElements() {
		ModelField mfName = getModelField(FIELD_NAME);
		ModelField mfDescription = getModelField(FIELD_DESCRIPTION);
		
		addListField(mfName,mfDescription);
		
		addDetailField(mfName,mfDescription);
		setRequired(FIELD_NAME);
		setRequired(FIELD_WFPHASES);
		setRequired(FIELD_WFEVENTS);
		
		super.prepareLayoutElements();
	}
	
	@Override
	protected Integer getDetailLayoutSectionColsPerRow() {
		return 1;
	}
	
	@Override
	protected void afterBuilderLayout() {
		ListLayout ll = createListLayout(model, "WFTemplate-Phases");
		ll.setDefaulted(false);
		ModelField mfPhase = getModelField(FIELD_WFPHASES);
		GridSection gs = ll.createGridSection();
		gs.setReadOnly(false);
		gs.setTagName("datagrid");
		gs.setNamePrefix(FIELD_WFPHASES);
		SectionElement element = (SectionElement) convertFieldToElement(mfPhase, true);
		element.setWidth(300);
		elementService.addSectionElement(gs, element);
		updateObject(model);
		ll = getListLayout(model.getName(), "WFTemplate-Phases");
		
		TabbedSection ts = elementService.createTabbedSection(getDefaultDetailLayout(), 3);
		
		FlatSection fsWfevents = ts.getTabPage(0);
		SectionElement seSelectedEvents = (SectionElement) convertFieldToElement(getModelField(FIELD_WFEVENTS), true);
		fsWfevents.safeChildren().add(seSelectedEvents);
		
		Map<String, String> exDataTypeProperties = new HashMap<String, String>();
		exDataTypeProperties.put("collection", "fn.service('espirit.eap.manager.workflow.wFTemplate').srcEventItems");
		seSelectedEvents.setExProperty(exDataTypeProperties);
		seSelectedEvents.setWidth(500);
		seSelectedEvents.setTagName("checkbox");
		seSelectedEvents.setParent(fsWfevents);
		
		FlatSection tabPage = ts.getTabPage(1);
		elementService.createSectionElement(tabPage, ll.getSortChildren());
		tabPage.setI18n(getModelField(FIELD_WFPHASES).getI18n());
		
		tabPage = ts.getTabPage(2);
		
		elementService.createSectionElement(tabPage, getModel(WFCondition.class).getDefaultDetailLayout().getSortChildren());
		tabPage.setI18n(getModelField(FIELD_WFCONDITION).getI18n());
		
		super.afterBuilderLayout();

		TabbedSection tsDetail = getDefaultDetailLayout().getTabbedSection();
		
		FlatSection fs1 = tsDetail.getTabPage(0);
		I18n i18n = i18nService.addI18n("凭证", "Ledger");
		fs1.setI18n(i18n.getKey());
		genericService.update(fs1);
		
		FlatSection fs2 = tsDetail.getTabPage(1);
		i18n = i18nService.addI18n("批准阶段", "Workflow Phases");
		fs2.setI18n(i18n.getKey());
		genericService.update(fs2);
	}
	
}
